<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        #app {
            padding: 20px;
        }

        .title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            line-height: 40px;
            margin-bottom: 20px;
        }

        .item {
            display: flex;
            justify-content: space-between;
            margin-bottom: 40px;
        }

        .left {
            display: flex;
        }

        .left img {
            width: 80px;
            margin-right: 20px;
        }

        .left .box h2 {
            font-size: 18px;
            line-height: 40px;
            margin-bottom: 15px;
        }

        .left .box .zuo {
            color: #ff5339;
            font-size: 16px;
        }

        .left .box .you {
            color: rgb(183, 182, 182);
            font-size: 12px;
            text-decoration: line-through;
        }

        .right {
            display: flex;
            align-items: center;
        }

        .right div {
            font-weight: 700;
            text-align: center;
            line-height: 30px;
            border-radius: 50%;
            width: 30px;
            height: 30px;
        }

        .right div:first-child {
            background-color: #00a6ff;
            color: white;
            margin-right: 5px;
        }

        .right div:last-child {
            background-color: white;
            border: 2px solid #00a6ff;
            line-height: 25px;
            margin-left: 5px;
        }

        .active {
            color: red;
            margin: 50px;
            text-align: center;
        }

        .box1 {
            display: flex;
            text-align: center;
            line-height: 40px;
        }

        .box1 div {
            width: 100px;
            color: lightcoral;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="title">
            <h2>已选商品</h2>
            <span @click="eliminate">清空</span>
        </div>
        <div class="list">
            <div class="item" v-for="(item,index) in list" :key="index">
                <div class="left">
                    <img :src="item.pic" alt="">
                    <div class="box">
                        <h2>{{ item.name }}</h2>
                        <p><span class="zuo">￥{{ item.pirce }} </span> <span class="you">￥{{ item.pirce }}</span></p>
                    </div>
                </div>
                <div class="right">
                    <div @click="reduce1(item)">-</div>
                    <div>{{ item.quantity }}</div>
                    <div @click="reduce(item)">+</div>
                </div>
            </div>
            <div class="active" v-show="list.length == 0">购物车中没有商品！！！</div>
            <div class="box1">
                <div class="totalprice"> 总价:{{ num }}</div>
                <div class="total">总数:{{ sum }}</div>
            </div>
        </div>
    </div>
    <script src="./vue-2.5.21.js"></script>
    <script>
        let v = new Vue({
            el: "#app",
            data: {
                list: [
                    { id: 1, pic: './img/pic8.jpg', name: '秋刀鱼/斤', pirce: 35.00, quantity: 1 },
                    { id: 2, pic: './img/pic9.jpg', name: '鲜大带鱼/条', pirce: 45.00, quantity: 1 },
                    { id: 3, pic: './img/pic10.jpg', name: '鲜活全母皮皮虾/斤', pirce: 85.00, quantity: 1 },
                ]
            },
            methods: {
                reduce(item) {
                    item.quantity >= 1 && item.quantity < 5 ? item.quantity++ : item.quantity = 4
                },
                reduce1(item) {
                    item.quantity > 1 && item.quantity <= 5 ? item.quantity-- : item.quantity = 1
                },
                eliminate() {
                    this.list = []
                },
            },
            computed: {
                num() {
                    let num = 0
                    this.list.forEach(item => {
                        num += item.quantity
                    });
                    return num
                },
                sum() {
                    let sum = 0
                    this.list.forEach(item => {
                        sum += (item.quantity * item.pirce)
                    })
                    return sum
                }
            },
            watch: {
                // datalist: {
                //     handler() {
                //         this.quantity = this.list.filter((item) => { return item.quantity })
                //     },
                //     deep: true
                // }
            }
        })
    </script>
</body>

</html>